package com.sd.h3.carrental.bean.impl;

import com.sd.h3.carrental.bean.intf.CarRental;
import com.sd.h3.carrental.bean.intf.CarRentalRemote;
import com.sd.h3.carrental.entity.CarEntity;

import javax.ejb.Stateless;
import javax.ejb.Remote;
import javax.persistence.EntityManager;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.List;
import java.util.ArrayList;


@Stateless
public class CarRentalSession implements CarRentalRemote {


    @javax.persistence.PersistenceContext
    EntityManager em;

   
    public void add(String make, String model, int isAvailable) {
        CarEntity car = new CarEntity();
        car.setIsAvailable(1);
        car.setMake(make);
        car.setModel(model);
        em.persist(car);
   }

    /*public void edit(CarEntity car) {
            em.merge(car);
    }*/

    public void delete(int id) {
            CarEntity car = (CarEntity)em.find(CarEntity.class, id);
            em.remove(car);
    }

    public CarEntity find(int pk){
        return (CarEntity)em.find(CarEntity.class, pk);
    }

    public List<CarEntity> findAvailableCars() {
        List<CarEntity> availableCars;
        availableCars = (List<CarEntity>)em.createNamedQuery("findAllAvailableCars").setParameter("isAvailable",1).getResultList();

        return availableCars;
    }
}
